package com.wechat;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * UDP接收消息演示
 * 接收信息
 */
public class UdpServerDemo02 {
    public static void main(String[] args) throws Exception {
        // 开放端口 接收消息
        DatagramSocket socket = new DatagramSocket(9090);
        // 接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        socket.receive(packet); // 阻塞接收
        // 输出接收地址信息
        System.out.println(packet.getAddress().getHostAddress());
        // 输出接收的信息
        byte[] data = packet.getData();
        String s = new String(data, 0, data.length);
        // 输出接收到的信息
        System.out.println(s);
        // 关闭资源流
        socket.close();
    }
}
